﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using Microsoft.Win32;
using Mono.Cecil;
using Mono.Cecil.Cil;

namespace HearthHelper
{
	public class PatchStone
	{
		//打炉石补丁
		public static bool PatchHearthStone(string path,int patchType)
		{
			try
			{
				if (path.Length <= 0) return false;
				string temp = path;
				File.Copy(path, path + ".bak", true);
				var resolver = new DefaultAssemblyResolver();
				resolver.AddSearchDirectory(temp.Replace("Assembly-CSharp.dll", ""));
				var assembly = AssemblyDefinition.ReadAssembly(path + ".bak",
					new ReaderParameters { AssemblyResolver = resolver });

				//窗口最小化
				if (patchType == 0)
				{
					foreach (TypeDefinition type in assembly.MainModule.Types)
					{
						if (type.Name == "GraphicsResolution")
						{
							foreach (MethodDefinition method in type.Methods)
							{
								if (method.Name == "IsAspectRatioWithinLimit")
								{
									foreach (Instruction ins in method.Body.Instructions)
									{
										if (ins.OpCode.Name == "brfalse.s")
										{
											ins.OpCode = OpCodes.Brtrue_S;
										}
									}
									break;
								}
							}
							break;
						}
					}
				}
				//去除广告
				else if (patchType == 1)
				{
					foreach (TypeDefinition type in assembly.MainModule.Types)
					{
						if (type.Name == "ViewCountController")
						{
							foreach (MethodDefinition method in type.Methods)
							{
								if (method.Name == "GetViewCount")
								{
									method.Body.Instructions[9].OpCode = OpCodes.Ldc_I4_1;
									break;
								}
							}
							break;
						}
					}
				}
				//去除特有开门
				else if (patchType == 2)
				{
					foreach (TypeDefinition type in assembly.MainModule.Types)
					{
						if (type.Name == "SplashScreen")
						{
							foreach (MethodDefinition method in type.Methods)
							{
								if (method.Name == "GetRatingsScreenRegion")
								{
									method.Body.Instructions[0].OpCode = OpCodes.Ret;
									break;
								}
							}
							break;
						}
					}
				}
				//去除金卡特效
				else if (patchType == 3)
				{
					foreach (TypeDefinition type in assembly.MainModule.Types)
					{
						if (type.Name == "Entity")
						{
							foreach (MethodDefinition method in type.Methods)
							{
								if (method.Name == "GetPremiumType")
								{
									method.Body.Instructions[11].OpCode = OpCodes.Ldc_I4_0;
									method.Body.Instructions[13].OpCode = OpCodes.Ldc_I4_0;
									break;
								}
							}
							break;
						}
					}
				}

				//保存文件
				assembly.Write(path);
				assembly.Dispose();
				return true;
			}
			catch (Exception e)
			{
                System.Windows.MessageBox.Show(
					e.ToString(),"",MessageBoxButton.OK);
				return false;
			}
		}
	}
}
